//
//  ZhengjiaUpadteViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/8.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "ZhengjiaUpadteViewController.h"
#import "LCActionSheet.h"
#import "Puser.h"
#import <UIButton+WebCache.h>
#import "SDPhotoBrowser.h"

@interface ZhengjiaUpadteViewController ()<LCActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,SDPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UIButton *back;
@property (weak, nonatomic) IBOutlet UIView *photoview;
@property (weak, nonatomic) IBOutlet UIButton *follow;
@property (weak, nonatomic) IBOutlet UIButton *handwith;
@property (nonatomic,strong) NSArray *btnarray;
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) UIImagePickerController *picker;
@end

@implementation ZhengjiaUpadteViewController
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"上传证件";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStyleDone target:self action:@selector(rightclikc)];
    self.back.tag=0;
    self.handwith.tag=2;
    self.follow.tag=1;
    [self setupimgae];
}

-(NSArray *)btnarray{
    if (_btnarray==nil) {
        _btnarray=@[self.back,self.follow,self.handwith];
    }
    return _btnarray;
}

-(void)rightclikc{
    
}
-(void)setupimgae{
    if ([self.user.power integerValue]<4) {
        [self.back sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:self.user.user_nameportf]] forState:UIControlStateSelected completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.back.selected=YES;
        }];
        [self.follow sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:self.user.user_nameportz]] forState:UIControlStateSelected completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.follow.selected=YES;
        }];
        [self.handwith sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:self.user.user_handwith]] forState:UIControlStateSelected completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.handwith.selected=YES;
        }];

    }else{
        [self.back sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:self.user.com_namef]] forState:UIControlStateSelected completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.back.selected=YES;
        }];
        [self.follow sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:self.user.com_namez]] forState:UIControlStateSelected completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.follow.selected=YES;
        }];
        [self.handwith sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:self.user.com_handwith]] forState:UIControlStateSelected completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.handwith.selected=YES;
        }];

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backclick:(id)sender {
    if (self.back.isSelected) {
        [self showImage:self.back.tag];
    }else{
}
}
- (IBAction)forclick:(id)sender {
    if (self.follow.isSelected) {
        [self showImage:self.follow.tag];
    }else{
        
    }
}
- (IBAction)allclick:(id)sender {
    if (self.handwith.isSelected) {
        [self showImage:self.handwith.tag];

    }else{
        
    }
   
}
-(void)imgpick:(NSInteger)tag isshowHas:(BOOL)isshow{
    UIButton *btn=self.btnarray[tag];
    NSArray *titlearray;
  
    if (isshow) {
        titlearray=@[@"重新上传",@"查看大图"];
       
    }else{
        titlearray=@[@"拍照",@"从相册中选择"];
       
    }
    LCActionSheet *sheet = [LCActionSheet sheetWithTitle:nil buttonTitles: titlearray redButtonIndex:-1 delegate:self];
    [sheet show];
    sheet.isTag=isshow;
    sheet.tag=tag;
}
-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%d",actionSheet.tag);
    if (buttonIndex==actionSheet.btnCount-1) {
        return;
    }
    if (actionSheet.isTag) {
        if (buttonIndex==0) {
            [self imgpick:actionSheet.tag isshowHas:NO];
        }else{
            [self showImage:actionSheet.tag];
        }
    }else{
        if (buttonIndex==0) {
            [self showCarmera:actionSheet.tag];
        }else{
            [self showimagepicker:actionSheet.tag];
        }
    }
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    UIButton *btn =self.btnarray[index];
    return [btn imageForState:UIControlStateSelected];
}

-(void)showImage:(NSInteger)index{
    SDPhotoBrowser *browser=[[SDPhotoBrowser alloc]init];
    
    browser.sourceImagesContainerView = self.photoview; // 原图的父控件
    browser.imageCount = self.btnarray.count ; // 图片总数
    browser.currentImageIndex = index;
    browser.delegate = self;
    [browser show];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
          UIButton *btn=self.btnarray[picker.view.tag];
          btn.selected=YES;
        [btn setImage:image forState:UIControlStateSelected];
        [self uploadimage:btn];
    }];
}

-(void)uploadimage:(UIButton *)btn{
    
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)showimagepicker:(NSInteger)tag{
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.picker.view.tag=tag;
    self.picker.sourceType=sourcheType;
    [self presentViewController:self.picker animated:YES completion:nil];
}

-(void)showCarmera:(NSInteger)tag{
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
    self.picker.view.tag=tag;
    self.picker.sourceType=sourcheType;
    [self presentViewController:self.picker animated:YES completion:nil];
}
-(UIImagePickerController *)picker{
    if (_picker==nil) {
        _picker=[[UIImagePickerController alloc]init];
        _picker.view.backgroundColor = [UIColor whiteColor];
        
        _picker.delegate =self;
        _picker.allowsEditing = YES;
    }
    return _picker;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
